package edu.rakovic.elearning.pages;

import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SetupRender;
import org.apache.tapestry5.corelib.components.Form;
import org.apache.tapestry5.ioc.annotations.Inject;

import edu.rakovic.elearning.beans.UserBean;
import edu.rakovic.elearning.interfaces.User;
import edu.rakovic.elearning.service.Users;

public class Registration {
	
	@Property
	@Persist("flash")
	private User user;

	@Inject
	private Users users;
	
	// "form" as component (check id) exists in tml file
	@Component
	private Form form;

	@SetupRender
	public void createObject() {
		user = new UserBean();
	}
	//validate our form
	void onValidateForm(){
		if (users.retreiveSingleUser(user.getUserName()) != null)
		{
			//record error (server-side) for our form.
			form.recordError("user name already exists, chose another one");
		}	
	}

	Object onSuccess() {
		//finally "save" user to "database" and retur to Index page.
		users.save(user);
		return Login.class;
	}

}
